home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /* nurb.c */
-
- #include <gl.h>
- #include <math.h>
- #include <stdio.h>
- #include <malloc.h>
- #include "defines.h"
- #include <device.h>
- #include "event.h"
- #include "data.h"
- #include "draw.h"
- #include "matrix.h"
- #include "file.h"
- #include "control.h"
- #include "slider.h"
- #include "nurb.h"
-
- static void update_childrens_control_points(
- struct node_struct *child,
- Matrix mat);
- static void transform_mesh(Mesh control, Matrix M);
-
-
- extern struct node_struct *root;
-
-
- /* Maximum size of nurbs polygon.
- Increase for better speed, decrease for better display. */
- float pixel_tolerance=50.0;
-
- /* Initializes the mesh to a plane with given z */
- void init_plane(Mesh control, Coord z)
- {
- int s,t;
- Coord x,spacing;
-
- spacing = 2.0/((float)(NUMPOINTS + 1));
-
- for(s = 0; s < NUMPOINTS; s++)
- {
- x = -1.0 + spacing;
- for(t = 0; t < NUMPOINTS; t++)
- {
- control[s][t][0] = x;
- control[t][s][1] = x;
- control[s][t][2] = z;
- x += spacing;
- }
- }
- }
-
-
- void draw_patch(Mesh control)
- {
- static double s[NUMKNOTS] =
- {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
-
- static double t[NUMKNOTS] =
- {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
-
- double d_control[NUMPOINTS][NUMPOINTS][3];
- int i,j,k;
-
- for(i = 0; i<NUMPOINTS; i++)
- for(j = 0; j<NUMPOINTS; j++)
- for(k=0; k<3; k++)
- d_control[i][j][k] = (double)control[i][j][k];
-
-
- bgnsurface();
- nurbssurface(
- sizeof(s) / sizeof(double), s,
- sizeof(t) / sizeof(double), t,
- 3 * sizeof(double),
- NUMPOINTS * 3 * sizeof(double),
- (double *) d_control,
- ORDER, ORDER,
- N_XYZ
- );
- endsurface();
- }
-
- void set_pixel_tolerance(void)
- {
- draw_display();
- }
-
- static long slider_window = -1;
- extern long snurb_window;
-
-
- static void close_slider_window(void)
- {
- winset(slider_window);
- winpush();
- winset(snurb_window);
- }
-
- void change_tolerance(void)
- {
-
- if (slider_window == -1)
- {
- keepaspect(8,1);
- slider_window = winopen("sliderwin");
- wintitle("Nurbs Resolution");
-
- RGBmode();
- doublebuffer();
- gconfig();
-
- add_slider("Maximum of %3.0f Pixels per Polygon", SLIDER_FLOAT, 1.0, 400.0,
- (sliderval *)&pixel_tolerance, 0.0, 0.0, 1.0, 1.0, NULL);
-
- add_event(slider_window, LEFTMOUSE, UP, set_pixel_tolerance, NULL);
-
- add_event(ANY, WINSHUT, slider_window, close_slider_window, NULL);
- /* WINSHUT queued in init_events */
- winset(snurb_window);
- }
- else
- {
- winset(slider_window);
- winpop();
- winset(snurb_window);
- }
- }
-
- void add_primitive(int primitive)
- {
- struct node_struct *new_object;
-
-
- deselect_children(root->child);
-
- switch(primitive)
- {
- case PLANE:
- read_file("plane.snb");
- break;
-
- case CUBE:
- read_file("cube.snb");
- break;
-
- case TEAPOT:
- read_file("teapot.snb");
- break;
-
- default:
- fprintf(stderr,"Uh Oh. Unknown primitive.\n");
- exit(0);
- }
-
- draw_display();
- }
-
-
-
- static void update_childrens_control_points(
- struct node_struct *child,
- Matrix mat)
- {
- if (child != NULL)
- {
- if (child->node_type == PATCH)
- transform_mesh(child->patch->control, mat);
-
-
- update_childrens_control_points(child->child, mat);
- update_childrens_control_points(child->sibling, mat);
- }
- }
-
-
- static void transform_mesh(Mesh control, Matrix M)
- {
- int s,t;
-
- for (s=0; s<NUMPOINTS; s++)
- for (t=0; t<NUMPOINTS; t++)
- transform(control[s][t],M);
- }
-
-
- void update_control_points(
- struct node_struct *object,
- Matrix mat)
- {
- if (object != NULL)
- {
- if (object->node_type == PATCH)
- transform_mesh(object->patch->control, mat);
-
-
- update_childrens_control_points(object->child, mat);
- }
- }
-
-
-
- void invert_selected_children(
- struct node_struct *child,
- Boolean parent_selected)
- {
- int s,t,c,dest_edge,p;
- Mesh temp;
- Boolean child_selected;
- struct node_struct *dest_patch;
- struct zip_struct *zipper, *temp_zipper;
-
- if (child != NULL)
- {
- child_selected = (parent_selected || child->selected);
-
- if ((child->node_type == PATCH) && child_selected)
- {
-
- /* Swap control points in s direction */
- for (s=0; s<4; s++)
- for (t=0; t<4; t++)
- for (c=0; c<3; c++)
- temp[s][t][c] = child->patch->control[s][t][c];
-
- for (s=0; s<4; s++)
- for (t=0; t<4; t++)
- for (c=0; c<3; c++)
- child->patch->control[s][t][c] = temp[3-s][t][c];
-
- /* If it was zipped, swap s indexes and steps on all edges */
- for (p=0; p<4; p++)
- if (child->patch->zipper[p]->zipped)
- {
- zipper = child->patch->zipper[p];
-
- zipper->s_index = 3 - zipper->s_index;
- zipper->s_step = -1 * zipper->s_step;
-
- zipper->i_s_index = 3 - zipper->i_s_index;
- zipper->i_s_step = -1 * zipper->i_s_step;
-
- }
-
-
- if (child->patch->zipper[1]->zipped)
- {
- dest_patch = child->patch->zipper[1]->patch;
- dest_edge = child->patch->zipper[1]->edge;
-
- dest_patch->patch->zipper[dest_edge]->edge = 3;
- }
-
- if (child->patch->zipper[3]->zipped)
- {
- dest_patch = child->patch->zipper[3]->patch;
- dest_edge = child->patch->zipper[3]->edge;
-
- dest_patch->patch->zipper[dest_edge]->edge = 1;
- }
-
- /* Swap what edges 1 and 3 were zipped to
- by swapping their zipper pointers */
-
- temp_zipper = child->patch->zipper[1];
- child->patch->zipper[1] = child->patch->zipper[3];
- child->patch->zipper[3] = temp_zipper;
- }
-
- invert_selected_children(child->child, child_selected);
- invert_selected_children(child->sibling, parent_selected);
- }
- }
-
-
-
-
- /* Turns all selected objects inside out */
- void invert_objects(void)
- {
- invert_selected_children(root->child,FALSE);
-
- draw_display();
- }
-
-
-
-
-
-